package com.book.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.book.common.constant.UserConstants;
import com.book.common.core.domain.entity.AppUser;
import com.book.common.utils.StringUtils;
import com.book.system.mapper.AppUserMapper;
import com.book.system.service.IAppUserService;
import org.springframework.stereotype.Service;

/**
 * @author Bigboss
 * @create 2024-10-19 16:20
 */
@Service
public class AppUserServiceImpl extends ServiceImpl<AppUserMapper, AppUser> implements IAppUserService {
    private final AppUserMapper appUserMapper;

    public AppUserServiceImpl(AppUserMapper appUserMapper) {
        this.appUserMapper = appUserMapper;
    }

    /**
     * 校验手机号码是否唯一
     * @param user 用户信息
     * @return
     */
    @Override
    public boolean checkPhoneUnique(AppUser user) {
        String userId = StringUtils.isEmpty(user.getId()) ? "-1" : user.getId();
        final AppUser appUser = appUserMapper.selectOne(new LambdaQueryWrapper<AppUser>()
                .eq(AppUser::getPhone, user.getPhone()).last("LIMIT 1"));
        if (StringUtils.isNotNull(appUser) && !appUser.getId().equals(userId))
        {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }
}
